// pages/msg_notice_details/msg_notice_details.js
let app = getApp()
var my_lid = ''
var my_notice = ''
Page({
    /**
     * 页面的初始数据
     */
    data: {
        array1: [{
                'LNAME': '单股'
            },
            {
                'LNAME': '双股'
            },
            {
                'LNAME': '三股'
            },
        ],
        my_table: [],
        zhuangtai:false,
    },
    // ListTouch触摸开始
    ListTouchStart(e) {
        this.setData({
            ListTouchStart: e.touches[0].pageX
        })
    },
    // ListTouch计算方向
    ListTouchMove(e) {
        this.setData({
            ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
        })
    },
    // ListTouch计算滚动
    ListTouchEnd(e) {
        if (this.data.ListTouchDirection == 'left') {
            this.setData({
                modalName: e.currentTarget.dataset.target
            })
        } else {
            this.setData({
                modalName: null
            })
        }
        this.setData({
            ListTouchDirection: null
        })
    },
    contract_add(e) {
        wx.navigateTo({
            url: '../msg_notice_add_details/msg_notice_add_details?lid=' + my_lid + '&list=' + '1'
        })
    },
    update_details() {
        wx.navigateTo({
            url: '../msg_notice_add_details/msg_notice_add_details?list=' + '2'
        })
    },
    bindPickerChange2(e) {
        this.setData({
            index1: e.detail.value,
        })
        for (var i = 0; i < this.data.array1.length; i++) {
            if (i == e.detail.value) {
                this.data.table[0].LNUMBER = this.data.array1[i].LNAME
                this.setData({
                    table: this.data.table
                })
            }
        }
    },
    bindinputip(e) {
        if (e.currentTarget.dataset.id == '1') {
            this.data.table[0].LPTNCODE = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.table[0].LCOMPANYID = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.table[0].LSALEOPID = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '4') {
            this.data.table[0].LITEMCODE = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '5') {
            this.data.table[0].LITEMNAME = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '6') {
            this.data.table[0].LITEMSTD = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '7') {
            this.data.table[0].LITEMCOMPOS = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '8') {
            this.data.table[0].LNIANDU = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '9') {
            this.data.table[0].LNDEN = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '11') {
            this.data.table[0].LWEIGHT = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '12') {
            this.data.table[0].LCURRENCYID = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '13') {
            this.data.table[0].LREMARK = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '14') {
            this.data.table[0].LUNIT = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '15') {
            this.data.table[0].LCOLORNAME = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '16') {
            this.data.table[0].LCOLORCODE = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '17') {
            this.data.table[0].LBATCH = e.detail.value
            this.setData({
                table: this.data.table
            })
        }
    },
    shanchu(e) {
        let that = this
        //删除从表信息
        wx.request({
            url: 'https://' + app.globalData.url + '/Contract/deleteContractDts',
            data: {
                LID: that.data.table[0].LID,
                LSEQ: e.currentTarget.dataset.lseq
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if (res.data.success == true) {
                    let list = that.data.my_table
                    list.splice(e.currentTarget.dataset.index, 1)
                    that.setData({
                        my_table: list
                    })

                    wx.showToast({
                        title: '成功',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '删除失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {}
                        }
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {}
                    }
                })
            }
        })
    },
    submission(e){
        let that = this
        wx.request({
            url: 'https://' + app.globalData.url + '/Contract/findContractTJ',
            data: {
                'MODULENO':300101,
                'LID':this.data.table[0].LID,
                'STATERUN':this.data.table[0].LSTATERUN,
                'LUSERCODE':app.globalData.lusercode,
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if(res.data.success == true) {
                    wx.navigateBack({
                        delta: 1
                    })
                    wx.showToast({
                        title: '成功',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    wx.showToast({
                        title: '失败',
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },
    scan(e) {
        let that = this
        let table = {
            table: that.data.table
        }
        wx.request({
            url: 'https://' + app.globalData.url + '/Contract/updateContract',
            data: {
                model: JSON.stringify(table)
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if (res.data.success == true) {
                    wx.navigateBack({
                        delta: 1
                    })
                    wx.showToast({
                        title: '成功',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '更新失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {}
                        }
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {}
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        console.log(options.lstaterun)
        app.globalData.lstaterun = options.lstaterun
        if(options.lstaterun == 12) {
            this.setData({
                zhuangtai:true,
            })
        }
        app.globalData.number = 0
        var date = new Date()
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var lw = new Date(date - 1000 * 60 * 60 * 24 * 30) //最后一个数字30可改，30天的意思

        var date1 = new Date()
        var date3 = new Date(date1)
        date3.setDate(date1.getDate() + 30)
        var enddate = date3.getFullYear() + '-' + (date3.getMonth() + 1 < 10 ? '0' + (date3.getMonth() + 1) : date3.getMonth() + 1) + '-' + (date3.getDate() < 10 ? '0' + date3.getDate() : date3.getDate())
        var lastY = lw.getFullYear()
        var lastM = lw.getMonth() + 1
        var lastD = lw.getDate()
        var startdate = lastY + '-' + (lastM < 10 ? '0' + lastM : lastM) + '-' + (lastD < 10 ? '0' + lastD : lastD) //三十天之前日期
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        var dateright = year + '-' + month + '-' + day

        that.setData({
            dateright: dateright
        })
        that.setData({
            dateleft: startdate
        })

        function notice() {
            wx.request({
                url: 'https://' + app.globalData.url + '/Contract/findContractAll',
                data: {
                    LID: options.lid
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/json;charset=utf-8'
                },
                success: function (res) {
                    if (res.data.success) {
                        if (res.data.obj[0].LSEQ == '' || res.data.obj[0].LSEQ == undefined) {
                            that.setData({
                                table: res.data.obj
                            })
                            for(var i = 0; i < that.data.array1.length; i ++) {
                                if(that.data.table[0].LNUMBER == that.data.array1[i].LNAME) {
                                    that.setData({
                                        index1:i
                                    })
                                }
                            }
                            my_lid = that.data.table[0].LID
                        } else {
                            that.setData({
                                table: res.data.obj,
                                my_table: res.data.obj
                            })
                            for(var i = 0; i < that.data.array1.length; i ++) {
                                if(that.data.table[0].LNUMBER == that.data.array1[i].LNAME) {
                                    that.setData({
                                        index1:i
                                    })
                                }
                            }
                            my_lid = that.data.table[0].LID
                            app.globalData.notice_lid = that.data.table[0].LID
                            app.globalData.notice_lseq = that.data.table[0].LSEQ
                            app.globalData.notice_lmakeopid = that.data.table[0].LMAKEOPID
                        }
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '获取数据失败',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {}
                            }
                        })
                    }
                },
                fail: function (res) {
                    that.setData({
                        loadModal: false
                    })
                    wx.showModal({
                        title: '提示',
                        content: '访问服务器出错',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {}
                        }
                    })
                }
            })
        }
        notice()
        my_notice = notice
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (app.globalData.number > 0) {
            my_notice()
        }
        app.globalData.number++
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {}
})